#pragma once

#include <windows.h>
#include <MMsystem.h>

namespace avlib{

#define MAX_BUFFERS 20


class AviFile;

class AudioRecorder{

private:
	UINT mDeviceId;
	HWAVEIN mhWaveIn;
	WAVEHDR mWaveHDR[MAX_BUFFERS];
	WAVEFORMATEX mFormat;

	//callback
	DWORD_PTR mWaveInCallback; 
	DWORD_PTR mWaveInCallbackParam;
	AviFile *m_avi;

	bool mbRecording;
	HANDLE hWaveInThread;
	//CRITICAL_SECTION cs;
	static int buf_cnt;

public:

	
	AudioRecorder();
	~AudioRecorder();

	bool process_buffer(WAVEHDR *pHdr);


	void set_param(UINT samplerate,unsigned char samplebits,bool is_stereo = true);

	//int open(UINT device,DWORD_PTR dwCallback, DWORD_PTR param);
	int open(UINT device);
	bool start(AviFile *avi);
	int stop();
	
	bool is_recording(){
		return mbRecording;
	}
	WAVEFORMATEX *get_format(){return &mFormat;};

	bool AudioRecorder::get_volume(int &volume);
	bool AudioRecorder::set_volume(int volume);

	
};
};